package LeetCode.minDiffInBST;

/**
 * @author Chen Hongyu
 */
public class Solution {

    Integer ans = 0;
    Integer prev = 0;

    public int minDiffInBST(TreeNode root) {
        prev=null;
        ans = Integer.MAX_VALUE;
        dfs(root);
        return ans;
    }

    public void dfs(TreeNode node) {
        if (node == null) {
            return;
        }
        dfs(node.left);
        if (prev != null) {
            ans = Math.min(ans, node.val - prev);
        }
        prev = node.val;
        dfs(node.right);
    }

}
